package com.sky.controller.admin;

import com.sky.config.RedisConfiguration;
import com.sky.constant.ShopStatusConstant;
import com.sky.result.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@RestController("adminShopController")
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 设置营业状态
     *
     * @param status
     * @return {@link Result}
     */
    @PutMapping("/{status}")
    public Result updateStatus(@PathVariable Integer status){
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        //更改redis的营业状态,无需返回值
        valueOperations.set(ShopStatusConstant.SHOP_STATUS,status);
        return Result.success();
    }

    /**
     * 获取营业状态
     *
     * @return {@link Result}
     */
    @GetMapping("/status")
    public Result queryStatus(){
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        //获取营业的状态
        Integer status = (Integer) valueOperations.get(ShopStatusConstant.SHOP_STATUS);
        return Result.success(status == null ? 0 : status);
    }
}
